import { Queue } from '$domain/service/queue';
import { JobStatus } from '../type';
import { BaseSimulationJobState } from './baseJobState';
import { SimulationQueueManager } from '../simulationQueueManager';
export class QueuingState extends BaseSimulationJobState {

  private simQueueRepo = SimulationQueueManager.Instance;
  
  constructor() { super('QueuingState'); }

  async doRun() {
    this.simJobRepo.updateJobStatus(this.jobId, JobStatus.Queuing);
    const jobQueue = this.simQueueRepo.createIfNotExist(this.product, new Queue<string>());
    await jobQueue.put(this.jobId);
    this.logger.debug(`😄 Job(JobId=${this.jobId}) added to queue(${this.product}), current queue length: ${jobQueue.currSize}`);
  }

  async doCancel() {

    this.logger.debug(`🔶 post cancel execute ==> job state is QueuingState, job status is ${this.simJobRepo.getJobStatus(this.jobId)}`);

    const queueName = this.product;
    const jobQueue = this.simQueueRepo.getByName(queueName);

    if (jobQueue) {
      const isSuccess = jobQueue.remove(this.jobId);
      if (isSuccess) {
        this.logger.debug(`remove job(jobId=${this.jobId}) from FIFO queue(name=${queueName}).`);
      } else {
        this.logger.debug(`❗️ There are no job(${this.jobId}) in the FIFO queue(name=${queueName}).`);
      }
    } else {
      this.logger.debug(`😄 driver queue not exist. queueName: ${this.product}`);
    }
  }
}